﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KnowledgeBaseIntermediary;


namespace SerializedData
{
    /// <summary>
    /// Represents a State.
    /// </summary>
    [Serializable()]
    public class SState
    {
        private String mName;
        private String mDescription;
        private String mTts;
        private bool mChecked;
        private StateDTO.PRIORITY mPriority;
        private List<long> mAutoCommands = new List<long>();
        private List<long> mScenes = new List<long>();
        private List<long> mManualCommands = new List<long>();
        private long mId;

        public SState(long id, String name, String description, String tts, bool isChecked, StateDTO.PRIORITY prio)
        {
            mId = id;
            mName = name;
            mDescription = description;
            mTts = tts;
            mChecked = isChecked;
            mPriority = prio;
        }

        public void addAutoCommand(SCommand command) 
        {
            if (!mAutoCommands.Contains(command.getId()))
                mAutoCommands.Add(command.getId());
        }
        public void addScene(SScene scene)
        {
            if (!mScenes.Contains(scene.getId()))
                mScenes.Add(scene.getId());
        }
        public void addManualCommand(SCommand command)
        {
            if (!mManualCommands.Contains(command.getId()))
                mManualCommands.Add(command.getId());
        }
        public void addAutoCommandById(String id)
        {
            if (!containsAutoCommandWithId(id))
                mAutoCommands.Add(long.Parse(id));
        }
        public void addManualCommandById(String id)
        {
            if (!containsManualCommandWithId(id))
                mManualCommands.Add(long.Parse(id));


        }

        public void addSceneById(String id)
        {
            if (!containsSceneWithId(id))
                mScenes.Add(long.Parse(id));
        }
        public void removeAutoCommand(SCommand command)
        {
            if (mAutoCommands.Contains(command.getId()))
                mAutoCommands.Remove(command.getId());
        }
        public void removeAutoCommandById(String id)
        {
            if (containsAutoCommandWithId(id))
                mAutoCommands.Remove(long.Parse(id));
        }
        public void removeScene(SScene scene)
        {
            if (mScenes.Contains(scene.getId()))
                mScenes.Remove(scene.getId());
        }
        public void removeSceneById(String id)
        {
            if (containsSceneWithId(id))
                mScenes.Remove(long.Parse(id));
        }
        public void removeManualCommand(SCommand command)
        {
            if (mManualCommands.Contains(command.getId()))
                mManualCommands.Remove(command.getId());
        }
        public void removeManualCommandById(String id)
        {
            if (containsManualCommandWithId(id))
                mManualCommands.Remove(long.Parse(id));
        }
        public void removeAllAutoCommands()
        {
            mAutoCommands = null;
            mAutoCommands = new List<long>();
        }
        public void removeAllManualCommands()
        {
            mManualCommands = null;
            mManualCommands = new List<long>();
        }
        public void removeAllScenes()
        {
            mScenes = null;
            mScenes= new List<long>();
        }


        public bool containsManualCommandWithId(String id)
        {
            foreach (long item in mManualCommands)
                if (("" + item).Equals(id))
                    return true;
            return false;
        }

        public bool containsAutoCommandWithId(String id)
        {
            foreach (long item in mAutoCommands)
                if (("" + item).Equals(id))
                    return true;
            return false;
        }

        public bool containsSceneWithId(String id)
        {
            foreach (long item in mScenes)
                if (("" + item).Equals(id))
                    return true;
            return false;
        }

        public List<SCommand> getManualCommands()
        {
            DataCollection dc = DataCollection.Instance;
            List<SCommand> retCommands = new List<SCommand>();
            List<long> tbd = new List<long>();

            foreach (long id in mManualCommands)
            {
                SCommand sCommand = dc.getCommandPool().Find(o => o.getId().Equals(id));
                if (sCommand == null)
                    tbd.Add(id);
                else
                    retCommands.Add(sCommand);
            }
            foreach (long id in tbd)
                if (mManualCommands.Contains(id))
                    mManualCommands.Remove(id);
            return retCommands;
        }
        public List<SCommand> getAutoCommands()
        {

            DataCollection dc = DataCollection.Instance;
            List<SCommand> retCommands = new List<SCommand>();
            List<long> tbd = new List<long>();

            foreach (long id in mAutoCommands)
            {
                SCommand sCommand = dc.getCommandPool().Find(o => o.getId().Equals(id));
                if (sCommand == null)
                    tbd.Add(id);
                else
                    retCommands.Add(sCommand);
            }
            foreach (long id in tbd)
                if (mAutoCommands.Contains(id))
                    mAutoCommands.Remove(id);
            return retCommands;
    
        }
        public List<SScene> getScenes()
        {
            DataCollection dc = DataCollection.Instance;
            List<SScene> retScenes = new List<SScene>();
            List<long> tbd = new List<long>();

            foreach (long id in mScenes)
            {
                SScene sScene = dc.getScenePool().Find(o => o.getId().Equals(id));
                if (sScene == null)
                    tbd.Add(id);
                else
                    retScenes.Add(sScene);
            }
            foreach (long id in tbd)
                if (mScenes.Contains(id))
                    mScenes.Remove(id);
            return retScenes;
    
        }

        public long getId()
        {
            return mId;
        }

        public String getName()
        {
            return mName;
        }

        public String getDescription()
        {
            return mDescription;
        }

        public String getTts()
        {
            return mTts;
        }

        public StateDTO.PRIORITY getPriority()
        {
            return mPriority;
        }

        public bool isChecked()
        {
            return mChecked;
        }

        public void setName(String name)
        {
            mName = name;
        }

        public void setDescription(String description)
        {
            mDescription = description;
        }

        public void setTts(String tts)
        {
            mTts = tts;
        }

        public void setPriority(StateDTO.PRIORITY priority)
        {
            mPriority = priority;
        }

        public void setChecked(bool ischecked)
        {
            mChecked = ischecked;
        }



    }
}
